package com.ipfsemc.ipfsemc.wx.menu;

import com.ipfsemc.ipfsemc.wx.WxLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.weixin4j.Weixin;
import org.weixin4j.component.MenuComponent;
import org.weixin4j.model.menu.ClickButton;
import org.weixin4j.model.menu.Menu;
import org.weixin4j.model.menu.SingleButton;
import org.weixin4j.model.menu.ViewButton;
import org.weixin4j.spring.WeixinTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class MenuUtil {

    @Autowired
    private WeixinTemplate weixinTemplate;

    @Autowired
    private WxLogin wxLogin;
    public  Map<String, Object> initMenu(String ctx){
        Map<String, Object> jsonData = new HashMap<String, Object>();
        jsonData.put("code", 0);
        jsonData.put("message", "微信菜单创建成功");
        try {

            Menu menu = new Menu();
            //创建菜单按钮
            List<SingleButton> buttons = new ArrayList<SingleButton>();
            menu.setButton(buttons);

            SingleButton btna1 = new ViewButton("IPFS介绍",  "https://mp.weixin.qq.com/mp/homepage?__biz=MzU0MTg4NjQ3Nw==&hid=2&sn=82ea339b35f64cd1f7de62f8d1153f8f&scene=18/");

            SingleButton btna2 = new ViewButton("FIL&介绍", "https://mp.weixin.qq.com/mp/homepage?__biz=MzU0MTg4NjQ3Nw==&hid=3&sn=5f31788aa0be05c011650a5e780503e2&scene=18/");

            SingleButton btna3 = new ViewButton("IPFS相关视频",  "https://mp.weixin.qq.com/mp/homepage?__biz=MzU0MTg4NjQ3Nw==&hid=4&sn=697a9f5778784fad54b8faffaf768b83&scene=18/");
            SingleButton btna4 = new ViewButton("IPFS周刊", "https://mp.weixin.qq.com/mp/homepage?__biz=MzU0MTg4NjQ3Nw==&hid=1&sn=b0383aace1505fac40a2c7473b05d6db&scene=18/");

            List<SingleButton> list=new ArrayList<SingleButton>();
            list.add(btna1);
            list.add(btna2);
            list.add(btna3);
            list.add(btna4);
            SingleButton btna=new SingleButton("IPFS&FIL");
            btna.setSubButton(list);
            buttons.add(btna);

//            SingleButton btnb = new ViewButton("IPFS易云",wxLogin.wxLogin());
            SingleButton btnb = new ViewButton("IPFS易云","http://www.ipfsemc.com/");
            buttons.add(btnb);

            SingleButton btnc1 = new ClickButton("科普访谈","101");
            SingleButton btnc2 = new ViewButton("闭门会议", "https://mp.weixin.qq.com/mp/homepage?__biz=MzU0MTg4NjQ3Nw==&hid=5&sn=5a97127971a45a42f6b6dbee5f24a630&scene=18/");
            SingleButton btnc3 = new ClickButton("直播访谈", "103");


            List<SingleButton> list2=new ArrayList<SingleButton>();
            list2.add(btnc1);
            list2.add(btnc2);
            list2.add(btnc3);
            SingleButton btnc=new SingleButton("IPFS&FIL");
            btnc.setSubButton(list2);
            buttons.add(btnc);

            System.out.println(menu.toJSONObject().toString());

            //创建自定义菜单
            Weixin weixin = weixinTemplate.getWeixinFactory().getWeixin();
            MenuComponent menu1 = weixin.menu();
            menu1.create(menu);
//            model.addAttribute("message", "微信菜单创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            jsonData.put("code", -1);
            jsonData.put("message", "微信菜单创建失败，原因：" + e.getMessage());
        }

        return jsonData;
    }
}
